package com.ukao.steward.ui.main;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ukao.steward.R;
import com.ukao.steward.adapter.MyMesssageAdapter;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.base.IAdapter;
import com.ukao.steward.base.IPage;
import com.ukao.steward.base.PageWrapper;
import com.ukao.steward.bean.MyMessageBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.T;
import com.ukao.steward.widget.MyLRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class MyMessageFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.recycler_views)
    MyLRecyclerView recyclerViews;
    Unbinder unbinder;
    TextView titles;
    @BindView(R.id.receipt_btn)
    TextView receiptBtn;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private PageWrapper mPageWrapper;
    private MyMesssageAdapter mMyMesssageAdapter;
    private  View view;
    private View mEmptyView;

    public static MyMessageFragment newInstance(String param1, String param2) {
        MyMessageFragment fragment = new MyMessageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

         view = inflater.inflate(R.layout.fragment_my_message, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        recyclerViews.forceToRefresh();
    }

    @Override
    protected void initView() {
        receiptBtn.setText("全部已读");
        receiptBtn.setVisibility(View.VISIBLE);
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
        title.setText("消息列表");
        List<MyMessageBean.ListBean> mData = new ArrayList<>();
        initLinearRecyclerView(recyclerViews);
        mEmptyView= view.findViewById(R.id.empty_view);
        mMyMesssageAdapter = new MyMesssageAdapter(_mActivity, mData);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mMyMesssageAdapter);
        recyclerViews.setAdapter(mLRecyclerViewAdapter);
        IPage page = new IPage() {

            @Override
            public void load(int geIndex, int pageSize) {

                pushMsgWork(geIndex, pageSize);

            }
        };
        mPageWrapper = new PageWrapper<>(mIAdapter, page);
        page.setPageSize(Constant.SEND_FACTORY_PAGE_SIZE);

    }

    IAdapter mIAdapter = new IAdapter() {
        @Override
        public void setDataSource(List data, boolean isNextLoad) {
            mEmptyView.setVisibility(CheckUtils.isEmpty(data)?View.VISIBLE:View.GONE);
            mMyMesssageAdapter.getDataList().clear();
            mMyMesssageAdapter.setDataList(data);
            mLRecyclerViewAdapter.notifyDataSetChanged();
            recyclerViews.setLoadMoreEnabled(isNextLoad);

        }

        @Override
        public void loadMoreFinish(List data, boolean isNextLoad) {

            mMyMesssageAdapter.addAll(data);
            mLRecyclerViewAdapter.notifyDataSetChanged();
            if (!isNextLoad) recyclerViews.setNoMore(!isNextLoad);

        }
    };


    @Override
    protected void initListener() {
        super.initListener();
        recyclerViews.setOnRefreshListener(mOnRefreshListener);
        recyclerViews.setOnLoadMoreListener(mOnLoadMoreListener);
        mLRecyclerViewAdapter.setOnItemClickListener(mOnItemClickListener);

    }


    private OnRefreshListener mOnRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {

            mLRecyclerViewAdapter.notifyDataSetChanged();
            mPageWrapper.loadPage(true);
        }
    };
    private OnLoadMoreListener mOnLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            mPageWrapper.loadPage(false);
        }
    };


    private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            MyMessageBean.ListBean mitem = mMyMesssageAdapter.getCount(position);
            start(MessageDetailsFragment.newInstance(mitem, ""));
        }
    };


    /**
     * 消息
     */
    public void pushMsgWork(int pageNum, int pageSize) {

        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("pageNum", pageNum + "");
        mHashMap.put("pageSize", pageSize + "");
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores().pushMsgWork(Constant.createParameter(mHashMap)),
                new ApiCallback<MyMessageBean>() {
                    @Override
                    public void onSuccess(MyMessageBean mBean) {
                        if (mBean.getHttpCode() == 200) {
                            MyMessageBean mData = mBean.getData();
                            mPageWrapper.addDataSource(mData.getList(), mData.getPages());
                        } else {
                            T.show(mBean.getMsg());

                        }

                    }

                    @Override
                    public void onFinish() {
                        recyclerViews.refreshFinish(Constant.SEND_FACTORY_PAGE_SIZE);

                    }
                });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.back_btn,R.id.receipt_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case   R.id.receipt_btn:
                if (mMyMesssageAdapter.getDataList().size()!=0)
                    allReadpushMsg();

                break;

        }
    }

    /**
     * 全部已读
     */
    public void allReadpushMsg() {
        showProgressDialog();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores().readAll(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean mBean) {
                        if (mBean.getHttpCode() == 200) {

                            for (MyMessageBean.ListBean listBean : mMyMesssageAdapter.getDataList()) {
                                listBean.setIsRead(1);
                            }
                            mLRecyclerViewAdapter.notifyDataSetChanged();

                        } else {
                            T.show(mBean.getMsg());

                        }

                    }

                    @Override
                    public void onFinish() {
                    dismissProgressDialog();

                    }
                });
    }
}
